<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Jul 1, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );
RLibImporter::cls( "xml.JSimpleXML" );

class BaseMenu extends Object {
	
	/**
	 * 
	 *
	 * @var JSimpleXML
	 */
	var $xml;
	
	function __construct() {
		$this->xml = new JSimpleXML ( null );
		return;
	}
	
	/**
	 * Get Children Node of XML document
	 *
	 * @return XML Tag
	 */
	function getChildren() {
		return $this->xml->document->children ();
	}
	
	/**
	 * Load XML File
	 *
	 * @param string $filePath - Path to XML file
	 * @return unknown
	 */
	function loadFile($filePath, $level = 0) {
		return $this->xml->loadFile ( $filePath, $level );
	}
	
	/**
	 * Render Menu
	 *
	 * @return HTML Menu
	 */
	function renderMenu() {
		
		return $this->getListMenu ( $this->getChildren () );
	}
	
	/**
	 * Get List of menu
	 *
	 * @param List of Menu
	 */
	function getListMenu($XMLTag, $selected = false) {
		static $j = 0;
		$j ++;
		$len = count ( $XMLTag );

		if (empty($XMLTag [0])) {
			return;
		}
		$currentLevel = $XMLTag [0]->level ();
		if ($currentLevel == 1)
			$attributes = ' id="menu" class="lev1Menu"';
		else {
			if ($selected == true) {
				$attributes = 'class="lev2Menu submenu"';
			} else {
				$attributes = 'class="lev2Menu submenu hide"';
			}
		}
		
		echo "<ul $attributes>";
		for($i = 0; $i < $len; $i ++) {
			$node = $XMLTag [$i];
			$tagName = $node->name ();
			if ($tagName == "appsMenu") {
				
				$clsName = get_class ( $this );
				$subMenu = new $clsName ( );
				
				$appName = $node->attributes ( "name" );
				
				$file = SYS_APP_PATH . $appName . DS . $appName . '.menu.xml';
				$subMenu->loadFile ( $file, $node->level () );
				
				$XMLTag [$i] = $subMenu->xml->document;
				$XMLTag [$i]->addAttribute ( "app", $appName );
			}
			$j ++;
			$k = $j + 1;
			$this->getOneMenu ( $XMLTag [$i] );
		}
		echo "</ul>";
		return;
	}
	
	/**
	 * Get One menu from list
	 *
	 * @param Menu link
	 */
	function getOneMenu($XMLTag) {
		$att = $XMLTag->attributes ();
		$title = $att ['title'];
		
		$selected = false;
		if (isset ( $att ['app'] )) {
			$resource = new SiteResource ( $att ['app'], $att ['act'], $att ['task'], $att ['option'] );
			$sysResource = SysRequest::getResource ();
			
			if ($sysResource->checkCover ( $resource )) {
				$selected = true;
			}
			
			$link = $resource->getLink ();
		} else if (isset ( $att ['link'] )) {
			$link = $att ['link'];
		}
		if (! empty ( $title )) {
			$this->printMenuByLevel ( $title, $link, $XMLTag->level (), $selected );
		}
		if (is_array ( $XMLTag->menu )) {
			$this->getListMenu ( $XMLTag->menu, $selected );
		}
		echo '</li>';
	}
	
	/**
	 * Print menu with level
	 *
	 * @param string - URL $menu
	 * @param Number - Level of Menu $level
	 */
	function printMenuByLevel($title, $link, $level, $selected) {
		echo $this->getMenuByLevel ( $title, $link, $level, $selected );
	}
	
	/**
	 * Format style for Menu by Level
	 *
	 * @param string - URL of Menu $menu
	 * @param Number - Level of menu $level
	 * @return Menu List with style
	 */
	function getMenuByLevel($title, $link, $level, $selected) {
		if ($selected) {
			$id = 'id="active_menu"';
			$cls = 'active';
		}
		$title = SysLanguage::_ ( $title );
		$link = RHtmlHelper::addLangForLink ( $link );
		if ($level == 1) {
			$menu = "<a $id class=\"mainlevel\" href=\"$link\">$title</a>";
			$cls = "$cls";
		} else {
			$menu = "<a $id class=\"sublevel\" href=\"$link\">$title</a>";
		}
		
		$menuList = '<li class="' . $cls . '">';
		$menuList .= $menu;
		//		$menuList .= "</li>";
		return $menuList;
	}
}
?>